class FontLoader {

  Tool parent;
  String fontFolder;

  GmlMultiParser parser;
  GmlMultiSaver saver;

  FontLoader(Tool parent, String fontFolder) {
    this.parent = parent;
    this.fontFolder = fontFolder;

    // GmlParser to load a Gml file
    parser = new GmlMultiParser(500, "", parent);
    parser.start();

    // GmlSaver to save a Gml
    saver = new GmlMultiSaver(500, "", parent);
    saver.start();
  }


  /**
   / Gets the list of all folders containing fonts
   / Returns a list of folders with absolute path
   */

  List<String> getAvailableFonts() {

    List<String> tmpFontList = FileUtils.scanFolder(fontFolder, "[^\\.]*+[^\\.]*+");
    return tmpFontList;
  }


  /**
   / Gets the list of gml files for a given font folder (absolute path)
   / Returns a list of Gml files (absolute path) filtered using the .gml extension
   */

  List<String> getGmlFontFiles(String fontFolder) {
    List<String> letterList = FileUtils.scanFolder(fontFolder, ".+\\.gml");
    return letterList;
  }


  /**
   / Loads the properties file containing the font info (artiste, name, info, keywords)
   /
   */
  // TODO


  /**
   / Loads the Gml files for a given font provided as absolute path
   / Returns it as a Font
   */

  void loadFont(String fontFolder) {
    List<String> fontFiles = getGmlFontFiles(fontFolder);
    parser.parse(fontFiles);
  }

  /**
   / Saves a font
   /
   */

  void saveFont(Font font) {
    // Build font path for each letter
    // Check taht artiste and font name are filled and correct (no special caracters)
    if (!font.artist.matches("[a-zA-Z0-9]+")) {
      println("Cannot save font: invalid letters in artist's name: "+font.artist);
      return;
      //return "error"
    }
    if (!font.name.matches("[a-zA-Z0-9]+")) {
      println("Cannot save: invalid letters in font's name: "+font.name);
      return;
      //return "error"
    }

    Map<String, Gml> stuffToSave = new HashMap<String, Gml>();
    String path = sketchPath("data")+"/"+font.artist+"-"+font.name;
    for (String letter : font.letters.keySet()) {
      // If no gml or empty, skip it
      if (font.letters.get(letter).empty()) {
        //println("Empty letter "+letter + " skipped");
        continue;
      }
      // Else, add it for saving
      Gml gml = font.letters.get(letter).gml;
      stuffToSave.put(path+"/"+letter+".gml", gml);
    }
    if (stuffToSave.size() > 0) {
      // Create folder
      File folder = new File(path);
      if (!folder.exists()) {
        try {
          if (folder.mkdirs())
            System.out.println("Directory Created");
          else
            System.out.println("Directory is not created");
        }
        catch(Exception e) {
          
        }
      }
      saver.save(stuffToSave);
    }
    else {
      println("Nothing to save, WTF");
    }
  }
}

